home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / slikmode.zip / SLIKMODE.C < prev    next >
C/C++ Source or Header  |  1991-03-26  |  6KB  |  216 lines

  1. /*
  2.     SlikMode - sets a serial port to any bps rate.
  3.  
  4.  
  5.     Copyright 1989 Software For Cash, Inc.
  6.  
  7.     Brian Henning
  8. */
  9.  
  10. #include    <bios.h>
  11. #include    <stdio.h>
  12. #include    <stdlib.h>
  13.  
  14. #define    BRDL 0                /* offset for baud rate divisor - lo    */
  15. #define    BRDH 1                /* offset for baud rate divisor - hi    */
  16. #define    IER    1                /* offset for interrupt enable register */
  17. #define LCR 3               /* offset for line control register     */
  18.  
  19. char *p = "Even";           /* printable interpretation of parity   */
  20.  
  21. int    main(int argc, char *argv[])
  22. {
  23.     int    i = 0,                /* general purpose integer variable    */
  24.         s = 1,                /* default stop bits                */
  25.         d = 7,                /* default data bits                */
  26.         n = 1;                /* default COM port number            */
  27.  
  28.     unsigned
  29.         data   = 0x02,           /* 7 bit data default  */
  30.         stop   = 0x00,           /* 1 stop bit default  */
  31.         parity = 0x30,           /* even parity default */
  32.         brdl   = 0x60,
  33.         brdh   = 0x00,          /* means 1200 baud is the default      */
  34.         lcr   = 0x00,           /* will contain stop,data,parity flags */
  35.         base[4]= {0x03f8,
  36.                 0x02f8,
  37.                 0x03e8,
  38.                 0x02e8},        /* base port for COM 1, 2, 3, and 4 */
  39.         baud   = 1200;          /* default baud rate                */
  40.  
  41.     long baud_rate = 0L;        /* calculated baud rate                */
  42.  
  43.     union
  44.         {
  45.         unsigned u;         /* 16 bits worth of flags, etc. */
  46.         struct
  47.             {
  48.             unsigned diskflag   :1; /* diskette drive installed      */
  49.             unsigned coproc     :1; /* coprocessor                   */
  50.             unsigned sysram     :2; /* RAM on system board           */
  51.             unsigned video      :2; /* startup video mode            */
  52.             unsigned disks      :2; /* drives 00=1, 01=2, 10=3, 11=4 */
  53.             unsigned dma        :1; /* 0=Yes, 1=No (1 for PC Jr.)    */
  54.             unsigned comports   :3; /* number of serial ports        */
  55.             unsigned game       :1; /* game adapter installed        */
  56.             unsigned modem      :1; /* internal modem                */
  57.             unsigned printers   :2; /* number of printers            */
  58.             } bits;
  59.         } equip;
  60.  
  61. /***************************************************************************/
  62. /* if no operands, then show the help text and exit                        */
  63. /***************************************************************************/
  64.  
  65.     if (argc < 2)
  66.         {
  67.         printf("\nSLIKMODE - Copyright 1989 Software For Cash, Inc.\n\n");
  68.         printf("Parameters:\n");
  69.         printf("\t/bn - BAUD RATE where n is a baud rate of your choice\n");
  70.         printf("\t/cn - COM  PORT where n is 1,2,3, or 4, if installed\n");
  71.         printf("\t/dn - DATA BITS where n is 7 or 8\n");
  72.         printf("\t/px - PARITY    where x is E, O, or N for EVEN, ODD, or NONE\n");
  73.         printf("\t/sn - STOP BITS where n is 1 or 2\n\n");
  74.         printf("\tDefaults are COM1:1200,E,7,1\n");
  75.         exit(0);
  76.         }
  77.  
  78. /***************************************************************************/
  79. /* eat all of the passed parms in order, building up the data word as req'd*/
  80. /***************************************************************************/
  81.  
  82.     for (i=1;i<argc;i++)
  83.         {
  84.         if (strnicmp("/d",argv[i],2) == 0)    /* data bits */
  85.             {
  86.             if (*(argv[i]+2) == '7')
  87.                 {
  88.                 data = 0x02;
  89.                 d = 7;
  90.                 continue;
  91.                 }
  92.  
  93.             if (*(argv[i]+2) == '8')
  94.                 {
  95.                 data = 0x03;
  96.                 d = 8;
  97.                 continue;
  98.                 }
  99.             printf("\aNumber of data bits (/d) is not 7 or 8\n");
  100.             exit(1);
  101.             }
  102.  
  103.         if (strnicmp("/s",argv[i],2) == 0)    /* stop bits */
  104.             {
  105.             if (*(argv[i]+2) == '1')
  106.                 {
  107.                 stop = 0x00;
  108.                 s = 1;
  109.                 continue;
  110.                 }
  111.  
  112.             if (*(argv[i]+2) == '2')
  113.                 {
  114.                 stop = 0x04;
  115.                 s = 2;
  116.                 continue;
  117.                 }
  118.  
  119.             printf("\aNumber of stop bits (/s) is not 1 or 2\n");
  120.             exit(1);
  121.             }
  122.  
  123.         if (strnicmp("/p",argv[i],2) == 0)    /* parity */
  124.             {
  125.             if ((*(argv[i]+2) | '\x20') == 'n')
  126.                 {
  127.                 parity = 0x00;
  128.                 p = "None";
  129.                 continue;
  130.                 }
  131.  
  132.             if ((*(argv[i]+2) | '\x20') == 'e')
  133.                 {
  134.                 parity = 0x30;
  135.                 p = "Even";
  136.                 continue;
  137.                 }
  138.  
  139.             if ((*(argv[i]+2) | '\x20') == 'o')
  140.                 {
  141.                 parity = 0x20;
  142.                 p = "Odd";
  143.                 continue;
  144.                 }
  145.  
  146.             printf("\aParity (/p) is not EVEN, ODD, or NONE\n");
  147.             exit(1);
  148.             }
  149.  
  150.         if (strnicmp("/b",argv[i],2) == 0)    /* baud rate */
  151.             {
  152.             if (baud = atoi(argv[i]+2))
  153.                 {
  154.                 baud_rate = (1843200L/(long)(baud*16L));
  155.  
  156.                 brdl = baud_rate % 0x0100;
  157.                 brdh = baud_rate / 0x0100;
  158.  
  159.                 continue;
  160.                 }
  161.  
  162.             printf("\aBaud rate (/b) cannot be zero\n");
  163.             exit(1);
  164.             }
  165.  
  166.  
  167.         if (strnicmp("/c",argv[i],2) == 0)    /* comm port */
  168.             {
  169.             equip.u = _bios_equiplist();
  170.  
  171.             n = atoi(argv[i]+2);
  172.  
  173.             if ((n < 1) || (n > equip.bits.comports))
  174.                 {
  175.                 printf("\aCOM port (/c) value invalid\n");
  176.                 exit(1);
  177.                 }
  178.             n--;     /* make port index relative to zero    */
  179.             }
  180.  
  181.         else
  182.             {
  183.             printf("\aUnknown parmameter %s\n",argv[i]);
  184.             exit(1);
  185.             }
  186.         }
  187.  
  188. /* request to diddle the baud rate divisor registers by setting LCR to 0x80    */
  189.  
  190.     outp(base[n]+LCR,0x80);
  191.  
  192.  
  193. /* stuff the requested values into the baud rate divisor registers */
  194.  
  195.     outp(base[n]+BRDH,brdh);
  196.     outp(base[n]+BRDL,brdl);
  197.  
  198.  
  199. /* stuff settings for data, stop, and parity bits into the LCR,
  200.    and enable the transmitter hold and data receiving registers */
  201.  
  202.     outp(base[n]+LCR,( (data | stop | parity) & 0x007f) );
  203.  
  204.  
  205. /* enable the UART for interrupts */
  206.  
  207.     outp(base[n]+IER,0x00);
  208.  
  209.  
  210. /* now brag about what we did */
  211.  
  212.     printf("SlikMode - COM%d:%d,%s,%d,%d\n",n+1,baud,p,d,s);
  213.  
  214.     exit(0);
  215. }
  216.